package com.dgut.finaldemo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.finaldemo.entity.Student;
import com.dgut.finaldemo.entity.StudentHealth;
import com.dgut.finaldemo.entity.Teacher;
import com.dgut.finaldemo.entity.TeacherHealth;
import com.dgut.finaldemo.mapper.StudentHealthMapper;
import com.dgut.finaldemo.mapper.StudentMapper;
import com.dgut.finaldemo.mapper.TeacherHealthMapper;
import com.dgut.finaldemo.mapper.TeacherMapper;
import com.dgut.finaldemo.vo.BarVO;
import com.dgut.finaldemo.vo.PieVO;
import com.dgut.finaldemo.vo.DataVO;
import com.dgut.finaldemo.vo.StudentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService{

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private StudentHealthMapper studentHealthMapper;

    @Autowired
    private TeacherHealthMapper teacherHealthMapper;

    @Override
    public DataVO<Student> findData(Integer page, Integer limit) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");

        IPage<Student> studentIPage = new Page<>(page,limit);
        IPage<Student> result = studentMapper.selectPage(studentIPage,null);

        dataVO.setCount(result.getTotal());

        List<Student> studentList = result.getRecords();
        List<StudentVO> studentVOList = new ArrayList<>();

        for(Student student:studentList){
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student,studentVO);
            if(student.getStatus()==1){
                studentVO.setStatus("正常");
            }else{
                studentVO.setStatus("停用");
            }
            studentVOList.add(studentVO);
        }
        dataVO.setData(studentVOList);
        return dataVO;


    }

    @Override
    public void addStudent(Student student) {
        studentMapper.insert(student);
    }

    @Override
    public Student queryStudentById(Integer sid) {
        Student student = studentMapper.selectById(sid);
        return  student;
    }

    @Override
    public void updateStudent(Student student) {
        studentMapper.updateById(student);
    }

    @Override
    public void deleteStudentById(Integer sid) {
        studentMapper.deleteById(sid);
    }

    @Override
    public DataVO<Student> searchStudent(Integer page, Integer limit, Student student) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");

        if(student.getUsername()==null)
            student.setUsername("");
        if(student.getName()==null)
            student.setName("");
        if(student.getStdclass()==null)
            student.setStdclass("");
        if(student.getPhone()==null)
            student.setPhone("");
        if(student.getAddress()==null)
            student.setAddress("");
        if(student.getClock()==null)
            student.setClock("");


        IPage<Student> studentIPage = new Page<>(page,limit);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if(student.getSid()==null){
            queryWrapper
                    .like("username",student.getUsername())
                    .like("name",student.getName())
                    .like("stdclass",student.getStdclass())
                    .like("phone",student.getPhone())
                    .like("address",student.getAddress())
                    .like("clock",student.getClock());
        }else{
            queryWrapper
                    .eq("sid",student.getSid())
                    .like("username",student.getUsername())
                    .like("name",student.getName())
                    .like("stdclass",student.getStdclass())
                    .like("phone",student.getPhone())
                    .like("address",student.getAddress())
                    .like("clock",student.getClock());
        }


        IPage<Student> result = studentMapper.selectPage(studentIPage,queryWrapper);

        dataVO.setCount(result.getTotal());

        List<Student> studentList = result.getRecords();
        List<StudentVO> studentVOList = new ArrayList<>();
        studentList.forEach(System.out::println);

        for(Student student1:studentList){
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student1,studentVO);
            if(student1.getStatus()==1){
                studentVO.setStatus("正常");
            }else{
                studentVO.setStatus("停用");
            }
            studentVOList.add(studentVO);
        }
        dataVO.setData(studentVOList);
        return dataVO;
    }

    @Override
    public List<PieVO> getPieClock() {
        List<PieVO> pieVOList = new ArrayList<>();
        PieVO pieVO1 = new PieVO();
        PieVO pieVO2 = new PieVO();
        PieVO pieVO3 = new PieVO();
        PieVO pieVO4 = new PieVO();

        QueryWrapper<Student> queryWrapper1 = new QueryWrapper<>();
        QueryWrapper<Student> queryWrapper2 = new QueryWrapper<>();
        QueryWrapper<Teacher> queryWrapper3 = new QueryWrapper<>();
        QueryWrapper<Teacher> queryWrapper4 = new QueryWrapper<>();
        queryWrapper1.eq("clock","已打卡");
        queryWrapper2.eq("clock","未打卡");
        queryWrapper3.eq("clock","已打卡");
        queryWrapper4.eq("clock","未打卡");

        pieVO1.setName("学生已打卡");
        pieVO1.setValue(studentMapper.selectCount(queryWrapper1));
        pieVOList.add(pieVO1);

        pieVO2.setName("学生未打卡");
        pieVO2.setValue(studentMapper.selectCount(queryWrapper2));
        pieVOList.add(pieVO2);

        pieVO3.setName("老师已打卡");
        pieVO3.setValue(teacherMapper.selectCount(queryWrapper3));
        pieVOList.add(pieVO3);

        pieVO4.setName("老师未打卡");
        pieVO4.setValue(teacherMapper.selectCount(queryWrapper4));
        pieVOList.add(pieVO4);


        System.out.println(pieVOList);
        return pieVOList;
    }

    @Override
    public BarVO getBarVO() {
        Date d=new Date();
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");


        QueryWrapper<StudentHealth> queryWrapper1 = new QueryWrapper<>();
        QueryWrapper<TeacherHealth> queryWrapper2 = new QueryWrapper<>();


        BarVO barVO = new BarVO();

        List<String> dateList = new ArrayList<>();
        List<Integer> studentClock = new ArrayList<>();
        List<Integer> studentNotClock = new ArrayList<>();
        List<Integer> teacherClock = new ArrayList<>();
        List<Integer> teacherNotClock = new ArrayList<>();

        for (int i=6;i>=0;i--){
            dateList.add(df.format(new Date(d.getTime() - (long)i * 24 * 60 * 60 * 1000)));
        }
        barVO.setDate(dateList);


        //学生打卡情况（一周）
        for (String dateStr:dateList) {

            queryWrapper1= queryWrapper1.eq("date",dateStr);

            queryWrapper2= queryWrapper2.eq("date",dateStr);

            Integer countStudentClock = studentHealthMapper.selectCount(queryWrapper1);
            Integer countStudentNotClock = studentMapper.selectCount(null)-countStudentClock;
            Integer countTeacherClock = teacherHealthMapper.selectCount(queryWrapper2);
            Integer countTeacherNotClock = teacherMapper.selectCount(null)-countTeacherClock;
            studentClock.add(countStudentClock);
            studentNotClock.add(countStudentNotClock);
            teacherClock.add(countTeacherClock);
            teacherNotClock.add(countTeacherNotClock);
            queryWrapper1=new QueryWrapper<>();
            queryWrapper2=new QueryWrapper<>();
        }

        barVO.setStudentClock(studentClock);
        barVO.setStudentNotClock(studentNotClock);
        barVO.setTeacherClock(teacherClock);
        barVO.setTeacherNotClock(teacherNotClock);
        return barVO;
    }

    @Override
    public Student queryStudentByUsername(String username) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        Student student=studentMapper.selectOne(queryWrapper);
        return student;
    }

}
